package com.example.testapiauto.yuxiang;

import com.alibaba.fastjson.JSONObject;
import com.example.testapiauto.app.serviceImp.myAssertImp;
import com.example.testapiauto.modules.testCase.utils.HttpClient;
import okhttp3.MediaType;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.testng.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author yuxiang
 * @version 1.0
 * @date 2021/4/23 9:24
 */
public class authorization {

    private static final Logger logger = LogManager.getLogger(authorization.class);
    static String TestUrl = resultsUtils.TestUrl_main;
    static myAssertImp myassertimp = new myAssertImp();
    public static final MediaType HJSON = MediaType.parse("application/json");


 /**
  *
  * @param
  * @author 假斯文
  * @date 2021/4/23 10:37
  * @return  获取token
  */
//    public static String TestEnvCookie(){
//        String TestUrl = Cookie.TestUrl;
//        String url = TestUrl+"/openApi/v1/message/id?id=145";
//        StringBuffer sb = new StringBuffer();
//        url = UrlEncodeing.GetRealUrl(url);
//        Response response =  doGet(url);
//        //获取响应信息中的头信息
//        Headers headers = response.headers();
//        //获取SET-Cookie的值
//        List<String> cookies = headers.values("SET-Cookie");
//        //遍历Cookies当中的值
//        for (String cookie : cookies){
//            sb.append(cookie.replace("Path=/","Path=/:"));
//        }
//        return sb.toString();
//    }


     /**
      *
      * @param
      * @author 假斯文
      * @date 2021/4/23 9:25
      * @return 获取签名 get
      */

     public static String TestEnvAuthenticationGET(){
         Object[] expect = new Object[2];
         expect[0] = "200";
         expect[1] = "V1.0.0";
         String url = TestUrl+"/usercenter/getSign";
         logger.info("请求url：" + url);
         Map<String,Object> headers = new HashMap<>();
         String body = "{\"queryString\":\"token=cdVzVrkOQL0AAgvQzIoj-7TP7U9tQX3uAEMyMDIxMDMzMTEzMjMxMg&clientId=C20210331132312&pageId=1\",\"httpMethod\":\"GET\"}";
         logger.info("请求参数："+body);
         String response = HttpClient.doPost_Json(body,url,headers);
         logger.info("响应结果：" + response);
         JSONObject act = JSONObject.parseObject(response);
         act.get("code");
         act.get("version");
         String[] JsonPach = new String[2];
         JsonPach[0] = "$.code";
         JsonPach[1] = "$.version";
         boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
         logger.info("响应断言：" + ResponseAssert);
         Assert.assertEquals(expect[0], act.getString("code"));
         return act.getString("data");
     }

      /**
       *
       * @param
       * @author 假斯文
       * @date 2021/5/7 16:49
       * @return 只带token
       */

      public static String TestEnvAuthenticationGETToken(){
          Object[] expect = new Object[2];
          expect[0] = "200";
          expect[1] = "V1.0.0";
          String url = TestUrl+"/usercenter/getSign";
          logger.info("请求url：" + url);
          Map<String,Object> headers = new HashMap<>();
          String body = "{\"queryString\":\"token=cdVzVrkOQL0AAgvQzIoj-7TP7U9tQX3uAEMyMDIxMDMzMTEzMjMxMg&clientId=C20210331132312\",\"httpMethod\":\"GET\"}";
          logger.info("请求参数："+body);
          String response = HttpClient.doPost_Json(body,url,headers);
          logger.info("响应结果：" + response);
          JSONObject act = JSONObject.parseObject(response);
          act.get("code");
          act.get("version");
          String[] JsonPach = new String[2];
          JsonPach[0] = "$.code";
          JsonPach[1] = "$.version";
          boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
          logger.info("响应断言：" + ResponseAssert);
          Assert.assertEquals(expect[0], act.getString("code"));
          return act.getString("data");
      }

    /**
     *
     * @param
     * @author 假斯文
     * @date 2021/5/7 16:49
     * @return 只带token
     */

    public static String TestEnvAuthenticationGETParams(){
        Object[] expect = new Object[2];
        expect[0] = "200";
        expect[1] = "V1.0.0";
        String url = TestUrl+"/usercenter/getSign";
        logger.info("请求url：" + url);
        Map<String,Object> headers = new HashMap<>();
        String body = "{\"queryString\":\"pageSize=10&pageNo=1\",\"httpMethod\":\"GET\"}";
        logger.info("请求参数："+body);
        String response = HttpClient.doPost_Json(body,url,headers);
        logger.info("响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("version");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.version";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
        return act.getString("data");
    }

      /**
       *
       * @param
       * @author 假斯文
       * @date 2021/4/23 10:52
       * @return 获取签名 post
       */

      public static String TestEnvAuthenticationPOST(){
          Object[] expect = new Object[2];
          expect[0] = "200";
          expect[1] = "V1.0.0";
          String url = TestUrl+"/usercenter/getSign";
          logger.info("请求url：" + url);
          Map<String,Object> headers = new HashMap<>();
          String body = "{\"queryString\":\"token=cdVzVrkOQL0AAgvQzIoj-7TP7U9tQX3uAEMyMDIxMDMzMTEzMjMxMg&clientId=C20210331132312&pageId=1\",\"httpMethod\":\"POST\"}";
          logger.info("请求参数："+body);
          String response = HttpClient.doPost_Json(body,url,headers);
          logger.info("响应结果：" + response);
          JSONObject act = JSONObject.parseObject(response);
          act.get("code");
          act.get("version");
          String[] JsonPach = new String[2];
          JsonPach[0] = "$.code";
          JsonPach[1] = "$.version";
          boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
          logger.info("响应断言：" + ResponseAssert);
          Assert.assertEquals(expect[0], act.getString("code"));

          return act.getString("data");
      }

       /**
        *
        * @param
        * @author 假斯文
        * @date 2021/4/27 15:37
        * @return  get请求，域名后面不跟参数
        */
    public static String TestEnvAuthenticationGETNull(){
        Object[] expect = new Object[2];
        expect[0] = "200";
        expect[1] = "V1.0.0";
        String url = TestUrl+"/usercenter/getSign";
        logger.info("请求url：" + url);
        Map<String,Object> headers = new HashMap<>();
        String body = "{\"queryString\":\"\",\"httpMethod\":\"GET\"}";
        logger.info("请求参数："+body);
        String response = HttpClient.doPost_Json(body,url,headers);
        logger.info("响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("version");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.version";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
        return act.getString("data");
    }


     /**
      *
      * @param
      * @author 假斯文
      * @date 2021/4/27 15:38
      * @return post请求 域名后面不拼接参数
      */
    public static String TestEnvAuthenticationPOSTNull(){
        Object[] expect = new Object[2];
        expect[0] = "200";
        expect[1] = "V1.0.0";
        String url = TestUrl+"/usercenter/getSign";
        logger.info("请求url：" + url);
        Map<String,Object> headers = new HashMap<>();
        String body = "{\"queryString\":\"\",\"httpMethod\":\"POST\"}";
        logger.info("请求参数："+body);
        String response = HttpClient.doPost_Json(body,url,headers);
        logger.info("响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("version");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.version";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
        return act.getString("data");
    }



 /**
  *
  * @param
  * @author 假斯文
  * @date 2021/4/25 9:38
  * @return
  */

 public static String data(){
     Object[] expect = new Object[2];
     expect[0] = "200";
     expect[1] = "V1.0.0";
     String url = TestUrl+"/usercenter/getSign";
     logger.info("请求url：" + url);
     Map<String,Object> headers = new HashMap<>();
     String body = "{\"queryString\":\"token=cdVzVrkOQL0AAgvQzIoj-7TP7U9tQX3uAEMyMDIxMDMzMTEzMjMxMg&clientId=C20210331132312\",\"httpMethod\":\"GET\"}";
     logger.info("请求参数："+body);
     String response = HttpClient.doPost_Json(body,url,headers);
     logger.info("响应结果：" + response);
     JSONObject act = JSONObject.parseObject(response);
     act.get("code");
     act.get("version");
     String[] JsonPach = new String[2];
     JsonPach[0] = "$.code";
     JsonPach[1] = "$.version";
     boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
     logger.info("响应断言：" + ResponseAssert);
     Assert.assertEquals(expect[0], act.getString("code"));
     String data = act.getString("data");
     String[] array1 = data.split("appId=C20210331132312,");
     for (int i = 0; i < array1.length; i++) {
         logger.info("所有的数据拆分为数组了。"+array1[i]);
     }
     return array1[0];
 }


}
